<?php
/**
 * Tooltip Plugin: Create tooltips for sections of wikitext.
 *
 *   syntax: <ttip::text>tooltip content<ttip>
 *
 * @brief      CSS-based inline toltips
 *
 *  This plugin allows to insert "tooltips" in DokuWiki text, so that
 *  hovering the mouse over the mrked text will reveal the tooltip.
 *  The wikitext that composes the tooltip is parsed for basic
 *  formatting only (underlines, italics and bolds). Otherwise,
 *  the plugin is perfectly usable.
 *  This plugin uses CSS only; no JavaScript or server side help
 *  (beyond the actual tag parsing) is used to harm any animal,
 *  in any way.
 */

if(!defined('DW_LF')) define('DW_LF',"\n");

if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
if(!defined('DOKU_PLUGIN_TOOLTIP_IMAGES')) define('DOKU_PLUGIN_TOOLTIP_IMAGES',DOKU_BASE.'lib/plugins/tooltip/images/');
require_once(DOKU_PLUGIN.'syntax.php');


class syntax_plugin_tooltip extends DokuWiki_Syntax_Plugin
{

	/**
	 * return some info
	 */
	function getInfo(){
		return array(
            'author' => 'Benjamin Eckstein',
            'email'  => 'umingo@googlemail.com',
            'date'   => '2011-08-06',
            'name'   => 'Tooltip Plugin',
            'desc'   => "Creates nice looking css tooltips". 
                       ': <ttip:text> tooltip </ttip>',
            'url'    => 'http://www.umingo.de',
		);
	}

	function getType()  {
		return 'formatting';
	}

	function getAllowedTypes()  {
		return array('substition','disabled','formatting');
	}

	function getPType() {
		return 'normal';
	}

	function getSort() {
		return 400;
	} // get by exp

	function connectTo($mode) {
		$this->Lexer->addEntryPattern(
                  "<ttip:.+?>(?=.+?</ttip>)", 
		$mode,
                  'plugin_tooltip');
	}

	function postConnect()  {
		$this->Lexer->addExitPattern(
                  "</ttip>",
                  'plugin_tooltip');
	}

	function handle($match, $state, $pos, &$handler) {

		$data= array();
		$data['state']= $state;

		switch ($state)  {
			case DOKU_LEXER_ENTER: {
				// here, we first look for an available ':' that
				// would indicate a 'style' parameter
				$match= substr($match, 6, -1);
				list($e1, $e2)= explode( ":", $match,2);
				// one ':' indicates only text folows, whereas two ':'
				// indicate there is also a style definition
				if ( empty($e2) ) {
					$e2= "";
				}
				else { // we have to look for things now
				}
				$data['class'] = $e1;
				$data['text']  = $e2;
				break;
			}
			case DOKU_LEXER_UNMATCHED: {
				// here, we simply pass the match because we want it to be
				// handled direclty by the DW parser
				$data['ttip']= $match;
				break;
			}
		} // end switch ($state)
		return $data;
	} // end function

	function render($mode, &$renderer, $data) {
		$state= $data['state'];
		$style= $data['class'];
		$text= $data['text'];
		$ttip= $data['ttip'];

		if($mode == 'xhtml') {
			/* let's do the jon thingie, that is,
			 create the HTML code that reveals the tooltip
			 */
			switch ($state) {
				case DOKU_LEXER_ENTER: {
					$title = $text;
					$imgurl = "";
					$css = "";
				
					if($style == "help"){
						$imgurl = DOKU_PLUGIN_TOOLTIP_IMAGES."help.png";
						$css = "help";
					}
					else if($style == "info"){
						$imgurl = DOKU_PLUGIN_TOOLTIP_IMAGES."info.png";
						$css = "info";
					}
					else if($style == "warning"){
						$imgurl = DOKU_PLUGIN_TOOLTIP_IMAGES."warning.png";
						$css = "warning";
					}
					else if($style == "critical"){
						$imgurl = DOKU_PLUGIN_TOOLTIP_IMAGES."critical.png";
						$css = "critical";
					}		
					else{				
						//todo make default icon & style	
						$imgurl = DOKU_PLUGIN_TOOLTIP_IMAGES."help.png";
						$css = "help";
					}								
					$out = "<a class='tooltip' href='#'><img src='".$imgurl."' alt='Help' height='24' width='24' />".'<span class="custom '.$css.'"><img src="'.$imgurl.'" alt="Help" height="48" width="48" /><em>'.$title.'</em>';
					$renderer->doc.= $out;
					break;
				}
				case DOKU_LEXER_UNMATCHED: {
					$renderer->doc.= $renderer->_xmlEntities($ttip);
					break;
				}
				case DOKU_LEXER_EXIT: {
					$renderer->doc.= '</span></a>';
					break;
				}
			} // end switch
			return true;
		} // end if (xhtml)

		else if ($mode == 'text') {
			// simple for the plaintext renderer (for things like search)
			switch ($state) {
				case DOKU_LEXER_ENTER: {
					$renderer->doc.= $mark. ' ';
					break;
				}
				case DOKU_LEXER_UNMATCHED: {
					$renderer->doc.= '[Tooltip: '. $content. '] ';
					break;
				}
				case DOKU_LEXER_EXIT: {
					break;
				}
			} // end switch
			return true;

		} // end if (text)

		// no more renderers
		return false;
	}
}
